package org.htzb.news.dao.impl;

import org.htzb.news.dao.C3P0Util;
import org.htzb.news.dao.UserDao;
import org.htzb.news.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl extends C3P0Util implements UserDao {
    @Override
    public User findUser(String username, String password)throws SQLException {
       Connection conn = getConnection();
       String sql = "select * from news_users where uname=? and upwd=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);
        ResultSet resultSet=preparedStatement.executeQuery();
        User user=null;
        if(resultSet.next()){
             user=new User();
             String uname=resultSet.getString("uname");
             String upwd=resultSet.getString("upwd");

             user.setUname(uname);
             user.setUpwd(upwd);
        }
        return user;
    }

    public static void main(String[] args) throws SQLException {
            UserDaoImpl userDao=new UserDaoImpl();
            User user= userDao.findUser("admin","admin");
            System.out.println(user);
    }
}
